<?php

namespace app\admin\controller\course;

use app\admin\model\User as ModelUser;
use app\common\controller\Backend;
use app\common\library\Auth;
use app\common\model\answer\Log;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Examination extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,username,nickname';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $acctLimit = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->get("filter", '');
            $op = $this->request->get("op", '');
            $filter = (array)json_decode($filter, true);
            $op = (array)json_decode($op, true);
            $filter = $filter ? $filter : [];
            $map = [];
            if (isset($filter['puser.nickname']) && $filter['puser.nickname']) {
                $nickname = $filter['puser.nickname'];
                //待发货 待评价 待收货 已完成
                unset($filter['puser.nickname'], $op['puser.nickname']);
                $ids = ModelUser::whereLike('nickname', '%' . $nickname . '%')->column('id');
                //一定要有
                $this->request->get(['filter' => json_encode($filter)], ['op' => json_encode($op)]);
                $map['parent_id'] = ['in', $ids];
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['lev'])
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->puser;
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
                $total_num = config('site.total_num') ? config('site.total_num') : 100;
                $count = Log::where('user_id', $v->id)->where('correct', 1)->count();
                $v->mark = bcmul($count / $total_num, 100, 0);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        Auth::instance()->delete($row['id']);
        $this->success();
    }
}
